package com.google.googlemarket.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.googlemarket.R;
import com.google.googlemarket.utils.Logger;

/**
 * Created by Administrator on 2017/9/19.
 */

public class LoadMoreListView extends ListView {

    private final View footerView;
    private final ProgressBar footer_progressbar;
    private final TextView footer_textview;
    private boolean loadingMore;
    private static final int STATE_LOADING_MORE = 1;
    private static final int STATE_RETRY = 2;
    private static final int STATE_NO_MORE_DATA = 3;
    private int currentState = STATE_LOADING_MORE;
    private OnloadingMoreListener mOnLoadingMoreListener;

    public LoadMoreListView(Context context, AttributeSet sttrs) {
        super(context, sttrs);
        footerView = View.inflate(context, R.layout.footer_view, null);
        footer_progressbar = (ProgressBar) footerView.findViewById(R.id.footer_progressbar);
        footer_textview = (TextView) footerView.findViewById(R.id.footer_textview);
        addFooterView(footerView);

        setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                        && getLastVisiblePosition() == getCount() - 1
                        && !loadingMore
                        && currentState==STATE_LOADING_MORE
                        && mOnLoadingMoreListener!=null) {
                    Logger.i(this, "item已经滚动到最后一条了");
                    loadingMore = true;
                    mOnLoadingMoreListener.onLoadingMore();
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
            }
        });

        footerView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (currentState == STATE_RETRY && mOnLoadingMoreListener != null) {
                    showLoadingMore();
                    mOnLoadingMoreListener.onClickRetry();
                }
            }
        });
    }

    public void onLoadMoreComplete() {
        loadingMore = false;
    }

    public void showLoadingMore() {
        currentState = STATE_LOADING_MORE;
        footer_progressbar.setVisibility(View.VISIBLE);
        footer_textview.setText("正在加载更多...");
    }

    public void showRetry() {
        Logger.i("loadMore","加载失败点击重试");
        currentState = STATE_RETRY;
        footer_progressbar.setVisibility(View.GONE);
        footer_textview.setText("加载失败，点击重试");
    }

    public void showNoMoreData() {
        currentState = STATE_NO_MORE_DATA;
        footer_progressbar.setVisibility(View.GONE);
        footer_textview.setText("没有更多数据了");
    }


    public void setOnLoadingMoreListener(OnloadingMoreListener listener) {
        this.mOnLoadingMoreListener = listener;
    }
}
